var app = new Vue({
    el: '.login',
    data() {
        return {
            emailnumber: "",
            password: '',
            istrue: false,
            maskfalse: false,
            theuser: '',
            information: '',
            loading:false
        };
    },
    methods: {
        quickLogin: function(e) {//回车及Enter登录
            if (e.keyCode == 13 || e.keyCode == 108) {
                this.login();
            }
        },
        getCookie(cookiename) {
        //获取cookie
            var arr,
                reg = new RegExp("(^| )" + cookiename + "=([^;]*)(;|$)");
            if ((arr = document.cookie.match(reg))) {
                return unescape(arr[2]);
            } else {
                return null;
            }
        },
        closeinfo() {
            document.getElementsByClassName('errorMessage')[0].style.display="none"
            this.theuser = "";
            this.information = ""
            let BODY = document.getElementsByTagName("body")[0]
            BODY.style.overflowY = ""
        },
        login() {//请求登录
            let that=this
            this.istrue = true
            let lang = this.getCookie("lang");
            if (this.emailnumber == "" || this.password == "") {
                let timer=setInterval(() => {
                    document.getElementsByClassName('errorMessage')[0].className='errorMessage'
                    clearInterval(timer)
                    timer=null
                },2000)
                document.getElementsByClassName('errorMessage')[0].className='errorMessage fadeInUp'
                if (lang == "en_US") {
                    this.theuser = "Input error"
                    this.information = "The account or password cannot be empty."
                } else if (lang == "zh_TW") {
                    this.theuser = "輸入錯誤"
                    this.information = "賬號或密碼不能為空"
                } else {
                    this.theuser = "输入错误"
                    this.information = "账号或密码不能为空"
                }
                this.istrue = false
                return
            }
            axios.post('/user/eamilcheck',{ email: this.emailnumber }).then(res => {
                if(res.data.code == 200){
                    let timer=setInterval(() => {
                        document.getElementsByClassName('errorMessage')[0].className='errorMessage'
                        clearInterval(timer)
                        timer=null
                    },2000)
                    document.getElementsByClassName('errorMessage')[0].className='errorMessage fadeInUp'
                    if (lang == "en_US") {
                        this.theuser = "Login Failed"
                        this.information = "Email does not exist"
                    } else if (lang == "zh_TW") {
                        this.theuser = "登錄失敗"
                        this.information = "郵箱不存在"
                    } else {
                        this.theuser = "登录失败"
                        this.information = "邮箱不存在"
                    }
                    this.istrue = false
                    return
                }else if(res.data.code == -10002){
                    let params = {
                        email: this.emailnumber,
                        password: this.password
                    }
                    axios.post("/user/signin", params).then(res => {
                        if (res.data.code == 200) {
                            let data = JSON.parse(res.data.data)
                            localStorage.setItem('Email', data.email)
                            localStorage.setItem('user', data.nickname)
                            localStorage.setItem('ID', data.userId)
                            localStorage.setItem('mytoken', data.token)
                            location.href = '/m'
                        } else if (res.data.code == -10005) {
                            let timer=setInterval(() => {
                                document.getElementsByClassName('errorMessage')[0].className='errorMessage'
                                clearInterval(timer)
                                timer=null
                            },2000)
                            document.getElementsByClassName('errorMessage')[0].className='errorMessage fadeInUp'
                            if (lang == "en_US") {
                            this.theuser="Password mistake"
                            this.information="The password you entered is incorrect."
                            } else if (lang == "zh_TW") {
                            this.theuser="密碼錯誤"
                            this.information="您輸入的密碼有誤"
                            } else {
                            this.theuser="密码错误"
                            this.information="您输入的密码有误"
                            }
                        }
                        this.istrue = false
                    })
                }
            })
            
        }
    },
    mounted(){
        this.loading=true
    }
})